#include "IBreakPointStateEvent.h"
#include "BreakPointService.h"

std::ostream &  IBreakPointStateEvent::log(std::ostream & out){
    return out;
}




BreakPointEndState * BreakPointEndState::inst = nullptr;
BreakPointEndState * BreakPointEndState::newInstance(){
    if(inst == nullptr){
        inst = new BreakPointEndState();
    }
    return inst;
}

int BreakPointEndState::start(BreakPointService * ctx){
    if(ctx != nullptr){
        ctx->loadFromFile();
        ctx->setState(BreakPointInitedState::newInstance());
        return 0;
    }
    return -1;
}
std::ostream &  BreakPointEndState::log(std::ostream & out){
    out << "\tstatus:end" << std::endl;
    return out;
}



BreakPointInitedState * BreakPointInitedState::inst = nullptr;
BreakPointInitedState * BreakPointInitedState::newInstance(){
    if(inst == nullptr){
        inst = new BreakPointInitedState();
    }
    return inst;
}
int BreakPointInitedState::stop(BreakPointService * ctx) {
    if(ctx != nullptr){ 
        ctx->saveToFile();
        ctx->setState(BreakPointEndState::newInstance());
        return 0;
    }
    return -1;
}
int BreakPointInitedState::connected(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->login();
        ctx->setState(BreakPointConnectedState::newInstance());
        return 0;
    }
    return -1;
}
std::ostream &  BreakPointInitedState::log(std::ostream & out){
    out << "\tstatus:inited" << std::endl;
    return out;
}


BreakPointConnectedState * BreakPointConnectedState::inst = nullptr;
BreakPointConnectedState * BreakPointConnectedState::newInstance(){
    if(inst == nullptr){
        inst = new BreakPointConnectedState();
    }
    return inst;
}
int BreakPointConnectedState::stop(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->saveToFile();
        ctx->setState(BreakPointEndState::newInstance());
        return 0;
    }
    return -1;
}
int BreakPointConnectedState::disconnected(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->logDisconnected();
        ctx->setState(BreakPointDisconnectedState::newInstance());
        return 0;
    }
    return -1;
}

int BreakPointConnectedState::receivedLoginResp(BreakPointService * ctx, const char * msg) {
    if(ctx != nullptr){
        ctx->handleLoginResp(msg);
        ctx->setState(BreakPointLoginedState::newInstance());
        return 0;
    }
    return -1;
}
int BreakPointConnectedState::sendMessage(BreakPointService * ctx, const char * msg) {
    if(ctx != nullptr){
        ctx->sendMessageAction(msg);
        return 0;
    }
    return -1;
}
std::ostream &  BreakPointConnectedState::log(std::ostream & out){
    out << "\tstatus:connected" << std::endl;
    return out;
}


BreakPointLoginedState * BreakPointLoginedState::inst = nullptr;
BreakPointLoginedState * BreakPointLoginedState::newInstance(){
    if(inst == nullptr){
        inst = new BreakPointLoginedState();
    }
    return inst;
}
int BreakPointLoginedState::timeout(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->saveToFile();
        return ctx->queryBreakPoint();
    }
    return -1;
}

int BreakPointLoginedState::receivedBreakPointResp(BreakPointService * ctx, const char * msg) {
    if(ctx != nullptr){
        return ctx->handleBreakPointResp(msg);
    }
    return -1;
}
int BreakPointLoginedState::receivedNormalResp(BreakPointService * ctx, const char * msg) {
    if(ctx != nullptr){
        return ctx->handleNormalResp(msg);
    }
    return -1;
}

int BreakPointLoginedState::disconnected(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->logDisconnected();
        ctx->setState(BreakPointDisconnectedState::newInstance());
        return 0;
    }
    return -1;
}
int BreakPointLoginedState::stop(BreakPointService * ctx){
    if(ctx != nullptr){
        ctx->saveToFile();
        ctx->setState(BreakPointEndState::newInstance());
        return 0;
    }
    return -1;
}

int BreakPointLoginedState::sendMessage(BreakPointService * ctx, const char * msg) {
    if(ctx != nullptr){
        return ctx->sendMessageAction(msg);
    }
    return -1;
}
std::ostream &  BreakPointLoginedState::log(std::ostream & out){
    out << "\tstatus:logined" << std::endl;
    return out;
}



BreakPointDisconnectedState * BreakPointDisconnectedState::inst = nullptr;
BreakPointDisconnectedState * BreakPointDisconnectedState::newInstance(){
    if(inst == nullptr){
        inst = new BreakPointDisconnectedState();
    }
    return inst;
}
int BreakPointDisconnectedState::connected(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->login();
        ctx->setState(BreakPointConnectedState::newInstance());
        return 0;
    }
    return -1;
}
int BreakPointDisconnectedState::stop(BreakPointService * ctx) {
    if(ctx != nullptr){
        ctx->saveToFile();
        ctx->setState(BreakPointEndState::newInstance());
        return 0;
    }
    return -1;
}
std::ostream &  BreakPointDisconnectedState::log(std::ostream & out){
    out << "\tstatus:logined" << std::endl;
    return out;
}
